package org.lex.swing.fontchooser;

import java.awt.Font;

public enum FontStyle {
	PLAIN("Plain", Font.PLAIN), BOLD("Bold", Font.BOLD), ITALIC("Italic", Font.ITALIC), BOLD_ITALIC("Bold italic", Font.BOLD | Font.ITALIC);

	public static final FontStyle DEFAULT_STYLE = PLAIN;

	public static FontStyle parseFromDiaplayName(String name) {
		FontStyle[] all = getAllStyles();
		for (FontStyle fs : all)
			if (fs.getDisplayName().equals(name))
				return fs;
		return DEFAULT_STYLE;
	}

	public static FontStyle parseFromStyle(int fontStyle) {
		if (fontStyle == BOLD_ITALIC.getStyle())
			return BOLD_ITALIC;
		FontStyle[] all = getAllStyles();
		for (FontStyle fs : all)
			if (fs.getStyle() == fontStyle)
				return fs;
		return DEFAULT_STYLE;
	}

	public static FontStyle parseFromFont(Font font) {
		return parseFromStyle(font.getStyle());
	}

	public String getDisplayName() {
		return this.displayName;
	}

	public int getStyle() {
		return this.style;
	}

	private static FontStyle[] all = null;

	private static synchronized FontStyle[] getAllStyles() {
		if (null == all)
			all = FontStyle.values();
		return all;
	}

	private String displayName = null;
	private int style = 0;

	private FontStyle(String displayName, int style) {
		this.displayName = displayName;
		this.style = style;
	}
}
